<script setup>
  import { ref, onMounted } from 'vue'
  import { list, allread } from '@/api/message'

  const isFlag = ref(false)
  // 任务列表数据
  const announceList = ref([])
  // 下一页页码
  const nextPage = ref(1)
  // 每页包含数据条数
  const pageSize = ref(10)

  const isEmpty = ref(false)
  const hasMore = ref(true)

  // 任务列表
  const getAnnounceList = async () => {
    const { code, data } = await list(200, nextPage.value, pageSize.value)
    // 检测接口是否调用成功
    if (code !== 200) return uni.utils.toast('获取通知失败，稍后重试!')
    // 渲染数据
    announceList.value = [...announceList.value, ...(data.items || [])]
    // 更新下一页页码
    nextPage.value = ++data.page
    // 是否还有更多数据
    hasMore.value = nextPage.value <= data.pages
    // 是否为空列表
    isEmpty.value = announceList.value.length === 0
  }
  // 上拉加载
  const onScrollToLower = () => {
    if (hasMore.avlue) return
    getAnnounceList()
  }
  onMounted(() => {
    getAnnounceList()
  })
  // 下拉刷新
  const onScrollViewRefresh = async () => {
    isFlag.value = true
    announceList.value = []
    nextPage.value = 1
    await getAnnounceList()
    isFlag.value = false
  }
  const allread1 = async () => {
    await allread(200)
    getAnnounceList()
  }
</script>
<template>
  <scroll-view
    @refresherrefresh="onScrollViewRefresh"
    @scrolltolower="onScrollToLower"
    :refresher-triggered="isFlag"
    refresher-enabled
    scroll-y
  >
    <view class="scroll-view-wrapper">
      <view class="message-action" @click="allread1">
        <text class="iconfont icon-clear"></text>
        全部已读
      </view>
      <uni-list :border="false">
        <uni-list-item
          v-for="announce in announceList"
          :key="announce.id"
          :to="`/subpkg_message/content/index?id=${announce.id}`"
          ellipsis="1"
          :title="announce.title"
          :right-text="announce.created"
        >
          <template v-slot:header>
            <text v-if="!announce.isRead" class="dot"></text>
          </template>
        </uni-list-item>
      </uni-list>
      <view v-if="isEmpty" class="message-blank">暂无消息</view>
    </view>
  </scroll-view>
</template>

<style lang="scss" scoped>
  @import './styles.scss';
</style>
